var express = require("express");
routes = require('./routes');
var http = require("http");
var path = require("path");
var mongoskin = require("mongoskin");
var dbUrl = process.env.MONGOHQ_URL || "mongodb://@localhost:27017/blog";
var db = mongoskin.db(dbUrl,{safe:true});
var collections = {
	articles:db.collection("articles"),
	users:db.collection("users")
}

var session = require("express-session");
var logger = require("morgan");
var errorHandler = require("errorhandler");
var cookieParser = require("cookie-parser");
var bodyParser = require("body-parser");
var methodOverride = require("method-override");

var app = express();
app.locals.appTitle = "blog-express";
app.use(function(req,res,next){
	if(!collections.articles || !collections.users) return next(new Error("No collections"));
	req.collections = collections;
	return next();
});
/*app.set("appName","hello-world");*/
app.set("port",3000);
app.set("views",path.join(__dirname,"views"));
app.set("view engine","jade"); 

app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser("3CCC4ACD-6ED1-4844-9217-82131BDCB239"));
app.use(session({secret:"2C44774A-D649-4D44-9535-46E296EF984F",resave:false,saveUninitialized:true}));
app.use(methodOverride('_method'));
app.use(require("stylus").middleware(__dirname + "/public"));
app.use(express.static(path.join(__dirname,"public")));
//用户认证中间件
app.use(function(req,res,next){
	if(req.session && req.session.admin){
		res.locals.admin = true;
	}
	next();	
});
//权限管理
var authorize = function(req,res,next){
	if(req.session && req.session.admin){
		return next();
	}else{
		return res.send(401);
	}
};
if("development" == app.get("env")){
	app.use(errorHandler());
}
 
//pages & routes
app.get("/",routes.index);
app.get("/login",routes.user.login);
app.post("/login",routes.user.authenticate);
app.get("/logout",routes.user.logout);
app.get("/admin",authorize,routes.article.admin);
app.get("/post",authorize,routes.article.post);
app.post("/post",authorize,routes.article.postArticle);
app.get("/articles/:slug",routes.article.show);

//rest api routes
app.get("/api/articles",routes.article.list);
app.post("/api/articles",routes.article.add);
app.put("/api/articles/:id",routes.article.edit);
app.delete("/api/articles/:id",routes.article.del);

app.all("*",function(req,res){
	res.sendStatus(404);
});
var server = http.createServer(app);
var boot = function(){
	server.listen(app.get("port"),function(){
		console.info("Express.js server listening on port"+app.get("port"));
	});
}
var shutdown = function(){
	server.close();
}
if(require.main === module){
	boot();
}else{
	console.info("Running app as a module");
	exports.boot = boot;
	exports.shutdown = shutdown;
	exports.port = app.get("port");
}
